package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;

import java.security.SecureRandom;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 *
 * @author chopper
 * 报款单数据同步接口
 *
 */

public class erpservlet implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(erpservlet.class);
    private final static String DES = "DES";
    public static final String charset = "UTF-8";

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        PropertiesUtil properties = new PropertiesUtil();
        def erpservletip = properties.readValue("erpservletip"); //获取配置文件erp接口IP
        def crmservletip = properties.readValue("crmservletip");//获取配置文件CRM接口IP
        def erpsecretkey = properties.readValue("erpsecretkey");//获取配置文件erp接口加密Key
        def receivebody = Util.getRequestBody(request, 'utf-8'); //获取报文
        log.debug("报款单入参:" + receivebody);
        JSONObject obj = JSONObject.fromObject(receivebody);
        //def method = obj.get("method");	//获取接口名称
        obj.put("secretkey", erpsecretkey);
        obj.put("timestamp", new Date().getTime() + ""); //加入时间戳
        /**
         if(method=='check_receiptbill'||method=='add_receiptbill'||method=='update_receiptstatus'){
         def timestamp = obj.get("timestamp");
         obj.put("content",encrypt(JSONObject.fromObject(obj.get("content")).toString(),timestamp));
         };
         */
        def url = crmservletip + "/ensign";
        def charset = "utf-8";
        def timeout = 100000;
        def enableSSLflag = 0;
        def requestinfo = Util.doHttpPost(url, JSONObject.fromObject(obj).toString(), charset, timeout, enableSSLflag);
        log.debug("ensign接口信息:" + url + "||" + requestinfo);

        log.debug("ERP接口地址:" + erpservletip);
        return Util.doHttpPost(erpservletip, requestinfo, charset, timeout, enableSSLflag);
    }

    public static String encrypt(String value, String key) throws Exception {
        byte[] bt = encryptByByte(value.getBytes(), key.getBytes());
        String strs = new BASE64Encoder().encode(bt);
        String encode = new String(org.apache.commons.codec.binary.Base64
                .encodeBase64(strs.getBytes(charset)));
        return encode;
    }

    private static byte[] encryptByByte(byte[] data, byte[] key)
            throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance("DES");

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }
}